﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using KeRui.BLL;
using KeRui.Common;
using System.Text;
using System.Collections.Generic;

public partial class Manage_Cms_VoteItemUpdate : System.Web.UI.Page
{
    protected KeRui.BLL.CmsVote bllCmsVote = new KeRui.BLL.CmsVote();
    protected KeRui.Model.CmsVote modelCmsVote = new KeRui.Model.CmsVote();
    protected KeRui.BLL.CmsVoteItem bllCmsVoteItem = new KeRui.BLL.CmsVoteItem();
    protected KeRui.Model.CmsVoteItem modelCmsVoteItem = new KeRui.Model.CmsVoteItem();
    public string Act = ManageHelper.PageAct();
    private ManageErrMsg nsb = new ManageErrMsg();
    protected string ActFlag = "edit";
    protected string ActName = "修改";
    protected int Count = 0;
    protected string DelItemID = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        GetInfo(PageRequest.GetInt("VoteID",-1));
        switch (ManageHelper.PageAct())
        {
                
            case "edit":
                Update();
                break;
            default:
                break;
        }
    }


    /// <summary>
    /// 修改方法
    /// </summary>
    private void Update()
    {
        int siteId = PageRequest.GetInt("siteID", -1);
        int VoteID = PageRequest.GetInt("VoteID", -1);
        KeRui.Model.CmsVoteItem CmsVoteItemModel = new KeRui.Model.CmsVoteItem();
        KeRui.BLL.CmsVoteItem BllCmsVoteItem = new KeRui.BLL.CmsVoteItem();
        KeRui.BLL.CmsVote bllCmsVote = new CmsVote();
        //组装数据
        modelCmsVote.VoteID = VoteID;
        modelCmsVote.VoteType = PageRequest.GetInt("radVoteType", -1);
        modelCmsVote.VoteLimit = PageRequest.GetInt("radVoteLimit", -1);
        modelCmsVote.Title = PageRequest.GetString("txtTitle");
        modelCmsVote.Content = PageRequest.GetString("txtContent");
        bllCmsVote.Update(modelCmsVote);

        int Count =int.Parse(PageRequest.GetString("txtMax"));
        for (int i = 0; i < Count;i++ )
        {
            if (PageRequest.GetString("HidID" + i).ToString() != string.Empty)
            {
                
                CmsVoteItemModel.Title = PageRequest.GetString("txtContent" + i);
                CmsVoteItemModel.VoteNum = int.Parse(PageRequest.GetString("txtNum" + i));
                CmsVoteItemModel.VoteItemID = int.Parse(PageRequest.GetString("HidID" + i).ToString());
                BllCmsVoteItem.Update(CmsVoteItemModel);
            }
            else
            {
               
                CmsVoteItemModel.Title = PageRequest.GetString("txtContent" + i);
                CmsVoteItemModel.VoteNum = int.Parse(PageRequest.GetString("txtNum" + i));
                CmsVoteItemModel.CreateTime = DateTime.Now;
                CmsVoteItemModel.VoteID = VoteID;
                int Result = BllCmsVoteItem.Add(CmsVoteItemModel);
            }
        }
        if (PageRequest.GetString("DelVoteItemID").ToString() != "")
        {
            string DelID = PageRequest.GetString("DelVoteItemID").ToString().Substring(0, PageRequest.GetString("DelVoteItemID").ToString().LastIndexOf(";"));
            string [] AllDelID = DelID.Split(';');
            for(int j=0;j<AllDelID.Length;j++)
            {
                BllCmsVoteItem.Delete(int.Parse(AllDelID[j]));
            }
        }
        bllCmsVote.UpdateSum(VoteID);
        Response.Redirect("VoteList.aspx?sucmsg=修改成功。&siteID=" + siteId + "");
    }

    /// <summary>
    /// 一下方法是记住一些页面回刷时的数据！
    /// </summary>
    /// <param name="voitID">主表ID（cmsVote）</param>
    private void GetInfo(int voitID)
    {
       modelCmsVote = bllCmsVote.GetModel(voitID);
       if (PageRequest.GetInt("Type", -1) != 1)
       {
           Count = bllCmsVoteItem.GetCount(voitID.ToString());
       }
       else
       {
           Count = int.Parse(PageRequest.GetString("txtMax"));
       }
    }

    /// <summary>
    /// 动态输出项目投票的子项
    /// </summary>
    /// <returns>返回组装好的HTML</returns>
    protected string GetVoteItem()
    {
        StringBuilder sb = new StringBuilder();
        List<KeRui.Model.CmsVoteItem> list = bllCmsVoteItem.GetList( PageRequest.GetInt("VoteID", -1).ToString());
        sb.Append(" <table width='100%' align='center' cellpadding='0' cellspacing='0' border='0'>");
        int i=0;
        int Total = PageRequest.GetInt("txtMax",-1);
        if (Total == -1)
        {
            Total = list.Count;
        }

        foreach (KeRui.Model.CmsVoteItem model in list)
        {
            if (0 == Total)
            {
                break;
            }
            if (i < Total)
            {
                sb.Append("<tr ><td align='left' width='90' height='26'>"
                    + "<img src='../pic/icons/help.gif' alt='标题' onclick=helpInf(this) /> " + (i + 1) + " 标题：</td>"
                    + "<td align='left' width='200'><input name='txtContent" + i + "' id='Text1' type='text' valid='required' errmsg='标题。' value='" + model.Title + "' /></td>"
                    + "<td align='left' width='90'><img src='../pic/icons/help.gif' alt='票数' onclick='helpInf(this)' /> 票数：</td>"
                    + "<td align='left'><input name='txtNum" + i + "' id='Text2' type='text' valid='required' errmsg='票数。' value='" + model.VoteNum + "' /><input type='hidden' name='HidID" + i + "' value='" + model.VoteItemID + "'></td></tr>");
            }
            else
            {
                DelItemID += model.VoteItemID.ToString() + ";";
            }
            i++;
        }
        for (int m = i; m <Total; m++)
        {
            sb.Append("<tr><td align='left' height='26'>"
                           + "<img src='../pic/icons/help.gif' alt='标题' onclick=helpInf(this) /> " + (m + 1) + " 标题：</td>"
                           + "<td align='left' ><input name='txtContent" + m + "' id='Text1' type='text' valid='required' errmsg='标题。' value='' /></td>"
                           + "<td align='left' ><img src='../pic/icons/help.gif' alt='票数' onclick='helpInf(this)' /> 票数：</td>"
                           + "<td align='left'><input name='txtNum" + m + "' id='Text2' type='text' valid='required' errmsg='票数。' value='0' /><input type='hidden' name='HidID" + m + "' value=''></td></tr>");
        }
        sb.Append("</table>");
        return sb.ToString();
    }
}
